
const Koa = require('koa');
const fs = require('fs');
const app = new Koa();
const request = require('koa/lib/request');
const formidable = require('koa-formidable')  //图片处理
const path = require('path')
const serve = require('koa-static')
var mysql = require('mysql');
const router = require("koa-router")();
const query = require("../mysql/query"); //引入异步查询方法
app.use(serve(__dirname))//设置静态文件
const nxlsx = require('node-xlsx')

//合同列表
var agreeList = async (ctx, next) => {
  var  Sql = 'SELECT agree_id,sign_time,applyer,has_settle,no_settle,is_complete FROM agree'
  let res = await query(Sql)
  ctx.body = {
    data:res,
    code:'200',
    msg:'成功'
  }
};


//合同录入
var agreeInput = async (ctx, next) => {
  let agree_id = ctx.request.body.agree_id
  let kind = ctx.request.body.kind;
  let price = ctx.request.body.price;
  let merchant = ctx.request.body.merchant || '';
  let applyer = ctx.request.body.applyer || '';
  let sign_time = ctx.request.body.sign_time;
  var  addSql = 'INSERT INTO agree(agree_id,kind,price,merchant,applyer,sign_time) VALUES(?,?,?,?,?,?)';
  var  addSqlParams = [agree_id,kind,price,merchant,applyer,sign_time];
  //添加以合同id命名的送货录入表
  var addTable = `CREATE TABLE IF NOT EXISTS ${agree_id} (id int NOT NULL primary key AUTO_INCREMENT comment 'primary key',name VARCHAR(255),price VARCHAR(255),inAgree VARCHAR(255),sendImg VARCHAR(255),priceImg VARCHAR(255),count INT,signer VARCHAR(255),times BIGINT)`
  await query(addTable)
  await query(addSql,addSqlParams)
  ctx.body = {
    code:'200',
    msg:'成功'
  }
};

//合同详情
var agreeDetail = async (ctx, next) => {
  let agree_id = ctx.request.body.agree_id
  var  Sql = `SELECT * FROM agree WHERE agree_id='${agree_id}'`
  let res = await query(Sql)
  ctx.body = {
    data:res,
    code:'200',
    msg:'成功'
  }
};

//查询合同编号
var searchAgreeId = async (ctx, next) => {
  let id = ctx.request.body.id
  var  Sql = `SELECT * FROM agree WHERE agree_id='${id}'`
  let res = await query(Sql)
  ctx.body = {
    data:res,
    code:'200',
    msg:'成功'
  }
};

//送货录入
var sendRecordInput = async (ctx, next) => {
  let sendImg = ctx.request.body.sendImg
  let priceImg = ctx.request.body.priceImg
  let list = ctx.request.body.list
  let tableId = ctx.request.body.tableId
  console.log("送货",ctx.request.body)
  list.forEach(async(item) => {
    let name = item.name;
    let price = item.price;
    let count = item.count;
    let times = new Date().getTime();
    let inAgree = '2';    //1在合同内 2不在
    var inSql = `SELECT kind FROM agree WHERE agree_id='${tableId}'`
    let res = await query(inSql)
    let inArr = res[0].kind.split('，')
    if(inArr.indexOf(name)>-1){
      inAgree='1'
    }else{
      inAgree='2'
    }
    var addSql = `INSERT INTO ${tableId}(name,price,inAgree,count,sendImg,priceImg,times) VALUES(?,?,?,?,?,?,?)`;
    var addSqlParams = [name,price,inAgree,count,sendImg,priceImg,times];
    await query(addSql,addSqlParams) 
  });
  ctx.body = {
    code:'200',
    msg:'成功'
  }
};

//送货记录表
var sendRecordList = async (ctx, next) => {
  let id = ctx.request.body.id;
  let name = ctx.request.body.name || '';
  let inAgree = ctx.request.body.inAgree || '';
  let pageSize = ctx.request.body.pageSize;
  let pageNum = ctx.request.body.pageNum;
  let sqlAll = `SELECT COUNT(*) num FROM ${id} WHERE name LIKE '%${name}%' AND inAgree LIKE '%${inAgree}%'`
  let sql = `SELECT * FROM ${id} WHERE name LIKE '%${name}%' AND inAgree LIKE '%${inAgree}%' LIMIT ${pageSize} OFFSET ${pageSize*(pageNum-1)} `
  let res = await query(sql) 
  let resp = await query(sqlAll) 
  ctx.body = {
    data:{
      dataList:res,
      all:resp[0].num,
    },
    code:'200',
    msg:'成功'
  }
};

module.exports = {
  'POST /agreeInput': agreeInput,
  'POST /agreeList': agreeList,
  'POST /agreeDetail': agreeDetail,
  'POST /searchAgreeId': searchAgreeId,
  'POST /sendRecordInput': sendRecordInput,
  'POST /sendRecordList': sendRecordList
};